//
//  CardbagVC.swift
//  CardBag
//
//  Created by hl on 2019/5/15.
//  Copyright © 2019 hlznj. All rights reserved.
//

import UIKit

class CardbagVC: BaseViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    var layout: UICollectionViewFlowLayout?
    var collection: UICollectionView?
    var dataSource = CardbayManager.getCardbagGroup()
    var addBtn: UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.initSubviews()

        NotificationCenter.default.addObserver(self, selector: #selector(reloadCollection), name: Notification.Name(UpdataNotifi), object: nil)
    }

    @objc func reloadCollection() {
        self.dataSource = CardbayManager.getCardbagGroup()
        self.collection?.reloadData()
    }

    func initSubviews() {
        let rightItem1 = UIBarButtonItem.init(image: UIImage.init(named: "addbtn")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(addMoreCardBtnAction))
        let rightItem2 = UIBarButtonItem.init(image: UIImage.init(named: "list_icon")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(showListVC))

        self.navigationItem.rightBarButtonItems = [rightItem1,rightItem2]

        self.layout = UICollectionViewFlowLayout.init()
        self.layout?.itemSize = CGSize.init(width: (kScreenW-30)/2, height: 100)
        self.layout?.minimumLineSpacing = 10
        self.layout?.minimumInteritemSpacing = 10
        self.layout?.headerReferenceSize = CGSize.init(width: kScreenW, height: 30)
        self.collection = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenW, height: kScreenH - kTabbarHeight), collectionViewLayout: self.layout!)
        self.collection?.delegate = self as UICollectionViewDelegate
        self.collection?.dataSource = self as UICollectionViewDataSource
        self.collection?.backgroundColor = UIColor.clear
        self.view.addSubview(self.collection!)
        self.collection?.register(CardbagCell.classForCoder(), forCellWithReuseIdentifier: "CardbagCell")
        self.collection?.register(CardBagCollectionHeader.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: "CardBagCollectionHeader")
    }

    //collection Delegate
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: CardbagCell = collection?.dequeueReusableCell(withReuseIdentifier: "CardbagCell", for: indexPath) as! CardbagCell
        let group: CardbagGroupModel = self.dataSource[indexPath.section]
        let cardbag = group.cardbags[indexPath.row]
        cell.label.text = cardbag.cardbagName
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let group: CardbagGroupModel = self.dataSource[indexPath.section]
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "CardBagCollectionHeader", for: indexPath) as! CardBagCollectionHeader
        header.nameLabel.text = group.groupName
        return header
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let group: CardbagGroupModel = self.dataSource[section]
        return group.cardbags.count
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 10, left: 10, bottom: 0, right: 10)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let group: CardbagGroupModel = self.dataSource[indexPath.section]
        let vc = CardbagDetailVC.init()
        vc.cardbagModel = group.cardbags[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }

    // 添加卡
    @objc func addMoreCardBtnAction() {
        let addCardbagVC = AddCardbagVC.init()
        self.navigationController?.pushViewController(addCardbagVC, animated: true)
    }

    //列表
    @objc func showListVC() {
        let cardbagListVC = CardbagListVC.init()
        self.navigationController?.pushViewController(cardbagListVC, animated: true)
    }
}
